<template>
  <el-row class="container">
    <el-col :span="15">
      <el-row>
        <el-col :span="5">
          <h2>登录账户管理</h2>
        </el-col>
      </el-row>
      <el-table
        border
        :data="listData.items">
        <el-table-column
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(scope.row.id)">编辑
            </el-button>
            <el-button
              type="danger"
              size="mini"
              @click="switchState(scope.row.id,scope.row.isValid)" v-if="scope.row.isValid === 1">无效化
            </el-button>
            <el-button
              type="success"
              size="mini"
              @click="switchState(scope.row.id,scope.row.isValid)" v-if="scope.row.isValid === 0">有效化
            </el-button>
            <add-customer-dialog :show.sync="editCustomer" @success="initData()" v-if="edittingId === scope.row.id" :dataItem="scope.row" :isedit="true"></add-customer-dialog>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="登录账号"
          align="center">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色"
          align="center">
          <!--<template slot-scope="scope">-->
            <!--<div v-if="scope.row.id !== edittingId">{{scope.row.name}}</div>-->
            <!--<el-input v-model="scope.row.name"  clearable size="mini" v-else></el-input>-->
          <!--</template>-->
        </el-table-column>
        <el-table-column
          prop="isValid"
          label="状态"
          align="center">
          <template slot-scope="scope">
            <div>{{scope.row.isValid===0?'无效':'有效'}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="createAt"
          label="创建日期"
          align="center">
          <template slot-scope="scope">
            <div>{{scope.row.createAt | format}}</div>
          </template>
        </el-table-column>
      </el-table>
      <app-pagination
        :currentPage.sync="listData.page"
        :size.sync="listData.size"
        :total="listData.total"
        @handleCurrentChange="initData()"
        @handleSizeChange="initData()"
      ></app-pagination>
      <div class="add_btn_box">
        <el-button @click="addCustomer = true" type="primary" size="mini">新增</el-button>
      </div>
    </el-col>
    <add-customer-dialog :show.sync="addCustomer" @success="initData()" v-if="addCustomer"></add-customer-dialog>
  </el-row>
</template>
<script>
  import ApiClient from '@/utils/ApiClient'
  import AppPagination from '@/components/AppPagination'
  import AddCustomerDialog from './AddCustomerDialog'

  export default {
    components: {
      'app-pagination': AppPagination,
      'add-customer-dialog':AddCustomerDialog
    },
    data() {
      return {
        listData: {
          page: 1,
          size: 10,
          items: null,
          total: null,
          totalPage: null,
        },
        addCustomer: false,
        editCustomer: false,
        //编辑条目id
        edittingId:null,
      }
    },
    methods: {
      initData() {
        ApiClient.get('/backend/users/list.do', {
          page: this.listData.page,
          size: this.listData.size,
        }).then(data => {
          //console.log(data)
          this.listData = data
        })
      },
      onSubmit() {
        this.listData.page = 1;
        this.listData.size = 10;
        this.initData()
      },
      //选择编辑条目
      handleEdit(id) {
        this.edittingId = id;
        this.editCustomer = true;
      },
      //提交编辑
      submitEdit(id,name){
        ApiClient.put(`/backend/exhibitType/update.do`,{id:id,name:name}).then(data =>{
          this.initData();
          this.edittingId=null;
        }).catch()
      },
      // 状态切换
      switchState(id,state) {
        let url = state ? `/backend/users/${id}/hide.do` : `/backend/users/${id}/show.do`;
        ApiClient.put(url).then(data => {
          this.initData()
        }).catch()
      },
    },
    mounted() {
      this.initData()
    }
  }
</script>
<style scoped lang="scss">
  .container {
    .add_btn_box {
      margin-top: 20px;
      .el-button {
      }
    }
    h2{
      margin-bottom: 20px;
    }
  }
</style>
